package com.bankichi.nfcwrench;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

/**
 * An {@link Activity} which handles a broadcast of a new tag that the device
 * just discovered.
 */
public class NFCWrenchAct extends Activity {

	static final String TAG = "NFCLever";

	/**
	 * This activity will finish itself in this amount of time if the user
	 * doesn't do anything.
	 */
	// static final int ACTIVITY_TIMEOUT_MS = 1 * 1000;

	TextView mTitle;
	Button Btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nfc_wrench);
		mTitle = (TextView) findViewById(R.id.title);
		resolveIntent(getIntent());
	}

	void resolveIntent(Intent intent) {
		// Parse the intent
		String action = intent.getAction();
		if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {

			byte[] tagId;
			String UID;
			tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

			UID = ByteArrayToHexString(tagId);
			
			//light_outdoor ~canteen_me
			if (UID.equalsIgnoreCase("2e755b58")) {
				Wrenches.AutoSyncOff();
				Wrenches.WiFiOff(this);
				Wrenches.AutoBrightOn(this);
				mTitle.setText(UID + " is the light outdoor card");
				endthis();
				
			//dim_outdoor ~traffic
			} else if (UID.equalsIgnoreCase("f6e707c0")) {
				Wrenches.AutoSyncOff();
				Wrenches.WiFiOff(this);
				Wrenches.AutoBrightOff(this);
				mTitle.setText(UID + " is the dim outdoor card");
				endthis();
				
			//home ~university_canteen
			} else if (UID.equalsIgnoreCase("8b6d1df7")) {
				Wrenches.AutoSyncOn();
				Wrenches.WiFiOn(this);
				Wrenches.AutoBrightOff(this);
				mTitle.setText(UID + " is the home card");
				endthis();
				
			//company ~canteen_Apple
			} else if (UID.equalsIgnoreCase("0e1ae855")) {
				Wrenches.AutoSyncOn();
				Wrenches.WiFiOn(this);
				Wrenches.AutoBrightOff(this);
				if (Wrenches.tryToConnectWifiBySSID(this, "p_wifi")) {
					mTitle.setText(UID
							+ " is the company card\nconnecting to p_wifi");
					Wrenches.startVPNSettings(this);
				} else {
					mTitle.setText(UID + " is the company card");
				}
				endthis();
				
			//others
			} else {
				mTitle.setText(UID + " is not used");
			}

		} else {
			Log.e(TAG, "Unknown intent " + intent);
			finish();
			return;
		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		setIntent(intent);
		resolveIntent(intent);
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle.setText(title);
	}

	private void endthis() {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				NFCWrenchAct.this.finish();
			}

		}, 2000);
	}

	private String ByteArrayToHexString(byte[] inarray) {
		int i, j, in;
		String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
				"B", "C", "D", "E", "F" };
		String out = "";

		for (j = 0; j < inarray.length; ++j) {
			in = (int) inarray[j] & 0xff;
			i = (in >> 4) & 0x0f;
			out += hex[i];
			i = in & 0x0f;
			out += hex[i];
		}
		return out;
	}

}
